package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {

        //1.根据name模糊分页查询套餐信息(从setmeal表)
        //1-1设置分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //1-2设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //1-3进行查询
        page = setmealMapper.selectPage(page, wrapper);

        //2 遍历
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //3.根据套餐categoryId去category表中查询对应信息
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                //4.根据套餐的id去中间表查询当前套餐对应菜品信息
                //LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                //wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                //List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                //setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;
    }

    //新增套餐
    @Override
    public void save(Setmeal setmeal) {
        //1.将基本信息保存到setmeal表
        setmealMapper.insert(setmeal);
        //2.遍历前端传入的菜品信息
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                //3.设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //4.将套餐菜品保存到setmeal_dish表
                setmealDishMapper.insert(setmealDish);

            }
        }
    }

    //删除套餐
    //1.删除套餐之前，要先判断当前要删除的这一批套餐是否有在售状态，一旦有一个在售，整个删除失败
    //2.删除套餐的时候，要将中间表的当前套餐相关数据一并删除
    @Override
    public void deleteByIds(Long[] ids) {
        //select count(0) from setmeal where status = 1 and in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus, 1);//状态为1(在售)
        wrapper.in(Setmeal::getId, ids);
        Integer num = setmealMapper.selectCount(wrapper);
        if (num > 0) {
            throw new CustomException("当前套餐处于在售状态，无法删除");
        }

        //2.删除套餐的时候，要将中间表的当前套餐相关数一并删除
        //2-1删除中间表
        //delete from setmeal_dish where setmeal_id in #{ids}
        LambdaQueryWrapper<SetmealDish> wapper1 = new LambdaQueryWrapper<>();
        wapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wapper1);

        //2-2删除套餐表
        //delete from setmeal where status = 1 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getStatus, 0);//状态为1(在售)
        wrapper2.in(Setmeal::getId, ids);
        setmealMapper.delete(wrapper2);
    }

    //批量停售 起售
    @Override
    public void updateByIds(Long[] ids, Integer status) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }

    //修改 回显套餐
    @Override
    public Setmeal findById(Long id) {
        //1.查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //2.根据setmeal中category-id从category表中查询name
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        //3.根据setmeal的id从setmeal-dish表中查询列表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }

    //修改套餐
    @Override
    public void update(Setmeal setmeal) {
        //1.根据主键更新菜品表信息
        setmealMapper.updateById(setmeal);

        //2.根据套餐id删除中间表中相关菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //根据分类id查询套餐
    @Override
    public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {

        //select * from setmeal where category_id = #{} and status = #{}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId)
                .in(Setmeal::getStatus, status);

        return setmealMapper.selectList(wrapper);
    }

    /*
     * 查询指定套餐的菜品列表
     * */
    @Override
    public List<Dish> getmealDish(Long id) {
        //1.从中间表中获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //2.创建一个集合用于封装 返回菜品信息
        ArrayList<Dish> dishArrayList = new ArrayList<>();

        //3.遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            //设置份数
            dish.setCopies(setmealDish.getCopies());
            //添加到返回集合
            dishArrayList.add(dish);
        }
        return dishArrayList;
    }
}
